package com.sxw.hw_student.system.controller;

import com.sxw.hw_student.system.entity.Teacher;
import com.sxw.hw_student.system.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;

@RestController
@RequestMapping("/api/teacher")
public class TeacherController {

    private final ITeacherService teacherService;

    @Autowired
    public TeacherController(ITeacherService teacherService) {
        this.teacherService = teacherService;
    }

    // 查询所有教师
    @GetMapping("/list")
    public List<Teacher> getAllTeachers() {
        return teacherService.getAllTeachers();
    }

    // 编辑更新教师信息（PUT方式）
    @PutMapping("/edit/{id}")
    public String updateTeacher(@PathVariable("id") Integer teacherNo, @RequestBody Teacher teacher) {
        teacher.setTeacherNo(teacherNo);
        boolean updated = teacherService.updateTeacher(teacher);
        return updated ? "更新成功" : "更新失败";
    }

    // 删除教师（DELETE方式）
    @DeleteMapping("/delete/{id}")
    public String deleteTeacher(@PathVariable("id") Integer teacherNo) {
        boolean deleted = teacherService.deleteTeacherById(teacherNo);
        return deleted ? "删除成功" : "删除失败";
    }

    }
